package cn.slt.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.slt.domian.MD_ClassDSID;
import cn.slt.domian.MD_Metadata;
import cn.slt.domian.MD_ObjDSID;
import cn.slt.domian.MD_libraryDSID;
import cn.slt.domian.SearchResult;
import cn.slt.service.ArrayService;
import cn.slt.service.MD_MetadataService;
import cn.slt.service.MD_ObjDSIDService;
import cn.slt.service.MD_classDSIDService;
import cn.slt.service.MD_libraryDSIDService;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


@Controller
@RequestMapping("ArrayController")
public class ArrayController {
	@Autowired
	private ArrayService service;
	@Autowired
	private MD_MetadataService md_MetadataService;
	@Autowired
	private MD_libraryDSIDService MD_libraryDSIDService;
	@Autowired
	private MD_classDSIDService MD_classDSIDService;
	@Autowired
	private MD_ObjDSIDService MD_ObjDSIDService;
	/**
	 * 复杂检索
	 * @param response
	 * @param str
	 */
	@RequestMapping("/ComplexSearch")
	@ResponseBody
	public void ComplexSearch(HttpServletResponse response,String str){
		try{
			JSONObject obj=JSONObject.parseObject(str.toString());
			String mdName = (String)obj.get("MetaName");
			String keyWords = (String)obj.get("keyword");
			String summary = (String)obj.get("abstracts");
			String buildUnit = (String)obj.get("buildUnit");
			String orderby = (String)obj.get("orderby");
			if(orderby.equals("0")){
				orderby = "";
			}
			List<SearchResult> list = service.queryComplexSearch(mdName,keyWords,
					summary,buildUnit,orderby);
			if(!list.isEmpty()){
				//System.out.println(list.get(0).toString());
				JSONArray jsonarray = new JSONArray();
				for(SearchResult li:list){
					JSONObject json = new JSONObject();
					json.put("objNameC",li.getObjNameC());
					json.put("objName",li.getObjName());
					json.put("tableNameC",li.getTableNameC());
					json.put("tableName", li.getTableName());
					json.put("firstname", li.getFirstname());//meta
					json.put("secondname", li.getSecondname());//meta
					json.put("thirdname", li.getThirdname());//lib
					json.put("unit", li.getUnit());//lib
				//	System.out.println(json.toString());
					jsonarray.add(json);
				}
				response.setCharacterEncoding("utf8");
				response.getWriter().write(jsonarray.toString());
				//System.out.println(jsonarray.toString());
			}
			else response.getWriter().write("0");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	@ResponseBody
	@RequestMapping("/showmetadata")
	public String showmetadata(HttpServletRequest request) {
		String[] string = request.getParameter("id").split("\\|");
		String owner = string[0];
		String location = string[1];
//		System.out.println(owner.equals(owner.trim())+"    "+owner);
//		System.out.println(location.equals(location.trim())+"    "+location);
		MD_Metadata md_Metadata = md_MetadataService.getone(location, owner);
		Map<String, String> map = new HashMap<String, String>();
		map.put("dsName", owner);
		map.put("location", location);
		MD_libraryDSID md_libraryDSID = MD_libraryDSIDService.gettemp(map);
		List<MD_ClassDSID>mClassDSIDs=MD_classDSIDService.getBySupdscode(md_libraryDSID.getDsCode());
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("metadata", md_Metadata);
		res.put("libsid", md_libraryDSID);
		res.put("classdsid",mClassDSIDs);
		//System.out.println(res.toString());
		return JSONArray.toJSONString(res);
	}

	@ResponseBody
	@RequestMapping("/showtablesAndcolunms")
	public String showtablesAndcolunms(HttpServletRequest request) {
		String string = request.getParameter("id");
		Integer dsCode = Integer.parseInt(string);
		MD_ClassDSID mDsid = MD_classDSIDService.getBydscode(dsCode);
		List<MD_ObjDSID> mObjDSIDs = MD_ObjDSIDService.getOjbDsids(dsCode);
		Map<String, Object> res = new HashMap<String, Object>();
		res.put("classdsid", mDsid);
		res.put("objdsid", mObjDSIDs);
		System.out.println(res.toString());
		return JSONArray.toJSONString(res);
	}
	
}
